package com.shoeshopping.service.joseph;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.shoeshopping.dao.BaseDao;
import com.shoeshopping.entity.User;


@Service("userService")
@Transactional
public class UserServiceImpl extends UserService {
	@Resource(name="baseDao")
	private BaseDao baseDao;
	
	@Override
	//@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public User checkLogin(User user) {
		List<Object> param = new ArrayList<Object>();
		param.add(user.getUsername());
		param.add(user.getPassword());
		String hql = "from User where username=? and password=?";
		List list = baseDao.find(hql, param);
		if(list!=null&&list.size()>0)
			return (User)list.get(0);
		return null;
	}
	
	@Override
	public boolean checkRegister(User user) { 
		//检查是否存在这个账户，如果存在则注册失败
		List<Object> param = new ArrayList<Object>();
		param.add(user.getUsername());
		String hql = "from User where username=?";
		List list =baseDao.find(hql, param);
		if(list!=null&&list.size()>0){
			return false;
		}else{
		//不存在该用户则注册该用户
		baseDao.save(user);
		 return true;
		}
	}

	@Override
	public void modify(User user) {
		baseDao.update(user);
		return;
	}

	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}
}
